package de.baumann.browser.model

import de.baumann.browser.api.net.PoolApiService
import de.baumann.browser.api.net.base.ApiBuilder
import de.baumann.browser.api.net.util.OtherHttpUtil
import de.baumann.browser.api.net.vo.CanCreatePool
import de.baumann.browser.api.net.vo.QiNiuToken
import de.baumann.browser.api.net.vo.Result
import de.baumann.browser.rx.RxSchedulers
import de.baumann.browser.utils.ErrorMsg
import de.baumann.browser.utils.StringUtil
import io.reactivex.Observable

class PoolModel : BaseModel() {

    fun createPoolGetSmsCode(phone: String,token:String): Observable<Result<Any>> {
        if (phone.isEmpty()) {
            return Observable.just(Result(ErrorMsg.TEL_EMPTY))
        }

        if (!StringUtil.isTel(phone)) {
            return Observable.just(Result(ErrorMsg.TEL_STYLE_ERROR))
        }
        return OtherHttpUtil.getSmsCode(phone, 7,token).compose(RxSchedulers.ioMain())
    }

    fun createPool(phone: String, code: String): Observable<Result<Any>> {
        if (phone.isEmpty()) {
            return Observable.just(Result<Any>(ErrorMsg.TEL_EMPTY))
        }

        if (!StringUtil.isTel(phone)) {
            return Observable.just(Result<Any>(ErrorMsg.TEL_STYLE_ERROR))
        }

        if (code.isEmpty()) {
            return Observable.just(Result<Any>(ErrorMsg.SMSCODE_EMPTY))
        }

        return poolApiService!!.createPool(phone, code, 86).compose(RxSchedulers.ioMain())
    }

    fun canCreatePool(): Observable<Result<CanCreatePool>> {
        return poolApiService!!.canCreatePool().compose(RxSchedulers.ioMain())
    }

    companion object {
        private var poolApiService: PoolApiService? = null

        init {
            poolApiService = ApiBuilder.buildService(PoolApiService::class.java)
        }
    }
}
